/*******************************************************************************************
 *  Action.c
 *
 *  Creating and managing Action object strucutres.
 *
 *******************************************************************************************/







/*******************************************************************************************
 *  INCLUDES :
 *******************************************************************************************/

/* System and library related : */
#include <stdlib.h>
#include <string.h>

/* Project related : */
#include "Wait.h"
#include "Repeat.h"
#include "ChangeSpeed.h"
#include "ChangeDirection.h"
#include "Accel.h"
#include "Vanish.h"
#include "Fire.h"
#include "FireRef.h"
#include "ActionRef.h"
#include "Action.h"







/*******************************************************************************************
 *  FUNCTIONS :
 *******************************************************************************************/

/*******************************************************************************************
 *  NEWACTION : Creates an empty Action object structure.
 *******************************************************************************************/
Action *newAction(void) {
	Action *a;

	/* Creating the container structure : */
	a = (Action *)malloc(sizeof(Action));


	/* Setting the object's type : */
	a->otype = action;


	/* Labeling the Action object : */
	a->label = NULL;


	/* For now, the action has no elements, so : */
	a->elementsNum = 0;
	

	/* Returning the still empty structure : */
	return a;

}




/*******************************************************************************************
 *  FREEACTION : destroys an Action object structure and all the objects it stores.
 *******************************************************************************************/
void freeAction(Action *a) {
	int i;					/* All purpose counter */

	/* Freing every object in the list according ... */
	/* ... to its type.                              */
	for(i=0; i<a->elementsNum; i++) {
		switch(((Object *)a->elements[i])->otype) {
			case waiting:									/* Wait */
				freeWait(a->elements[i]);		
				break;
			case repeat:									/* Repeat */
				freeRepeat(a->elements[i]);		
				break;
			case changeSpeed:								/* ChangeSpeed */
				freeChangeSpeed(a->elements[i]);
				break;
			case changeDirection:							/* ChangeDirection */
				freeChangeDirection(a->elements[i]);
				break;
			case accel:										/* Accel */
				freeAccel(a->elements[i]);
				break;
			case vanish:									/* Vanish */
				freeVanish(a->elements[i]);
				break;
			//case fire:										/* Fire */
			//	freeFire(a->elements[i]);
			///	break;
			case fireRef:									/* FireRef */
				freeFireRef(a->elements[i]);
				break;
			//case action:									/* Action */
			//	freeAction(a->elements[i]);
			//	break;
			case actionRef:									/* ActionRef */
				freeActionRef(a->elements[i]);
				break;
			default:
				break;
		}
	}

	free(a);
}							/* Freeing the object itself */



/*******************************************************************************************
 *  SETACTIONLABEL : sets an action label.
 *******************************************************************************************/
void setActionLabel(Action *a, char *l) {

	a->label = malloc(MAX_LABEL_LENGTH);
	
	strcpy(a->label, l);

}



/*******************************************************************************************
 *  PUSHACTIONELEMENT : pushes a new element object on the action's list :
 *******************************************************************************************/
void pushActionElement(Action *a, void *e) {
	a->elements[a->elementsNum] = e;
	a->elementsNum++;
}
